using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class BtbElseDTO : INotifyPropertyChanged
    {
        private ObservableCollection<BtbElseDetailDTO> _BtbDetails;
        private String _Description;
        private int _No;
        private String _NoBtb;
        private PurchaseOrderDTO _PurchaseOrder;
        private SupplierDTO _Supplier;


        private DateTime? _TglBtb;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private int _Status;

        public BtbElseDTO()
        {
            TglBtb = DateTime.Now.Date;
        }

        [DataMember]
        public String NoBtb
        {
            get { return _NoBtb; }
            set
            {
                _NoBtb = value;
                RaisePropertyChanged("NoBtb");
            }
        }

        [DataMember]
        public DateTime? TglBtb
        {
            get { return _TglBtb; }
            set
            {
                _TglBtb = value;
                RaisePropertyChanged("TglBtb");
            }
        }


        [DataMember]
        public PurchaseOrderDTO PurchaseOrder
        {
            get { return _PurchaseOrder; }
            set
            {
                _PurchaseOrder = value;
                RaisePropertyChanged("PurchaseOrder");
            }
        }


        [DataMember]
        public SupplierDTO Supplier
        {
            get { return _Supplier; }
            set
            {
                _Supplier = value;
                RaisePropertyChanged("Supplier");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<BtbElseDetailDTO> BtbDetails
        {
            get { return _BtbDetails; }
            set
            {
                if (value == null || ReferenceEquals(value,BtbDetails)) return;
                _BtbDetails = value;
                RaisePropertyChanged("BtbDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public int Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            BtbDetails.CollectionChanged += BtbDetails_CollectionChanged;
            int number = 0;
            foreach (BtbElseDetailDTO DetailDto in BtbDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void BtbDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (BtbElseDetailDTO DetailDto in BtbDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}